<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Http基本概念、原理 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Http基本概念、原理</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">January 8, 2021&nbsp;&nbsp;1:25:56</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="HTTP的定义"><a href="#HTTP的定义" class="headerlink" title="HTTP的定义"></a>HTTP的定义</h2><p>Hypertext Transfer Protocol，超⽂本传输协议，和HTML (Hypertext MarkupLanguage超⽂本标记语⾔)⼀起诞⽣，⽤于在⽹络上请求和传输HTML内容。<br>超⽂本，即「扩展型⽂本」，指的是HTML中可以有链向别的⽂本的链接（hyperlink）。</p>
<p>⽤户输⼊地址后回⻋或点击链接-&gt;浏览器拼装HTTP报⽂并发送请求给服务器-&gt;服务器处理请求后发送响应报⽂给浏览器-&gt;浏览器解析响应报⽂并使⽤渲染引擎显示到界⾯</p>
<h2 id="HTTP的⼯作⽅式"><a href="#HTTP的⼯作⽅式" class="headerlink" title="HTTP的⼯作⽅式"></a>HTTP的⼯作⽅式</h2><h3 id="浏览器："><a href="#浏览器：" class="headerlink" title="浏览器："></a>浏览器：</h3><p>⽤户输⼊地址后回⻋或点击链接-&gt;浏览器拼装HTTP报⽂并发送请求给服务器-&gt;服务器处理请求后发送响应报⽂给浏览器-&gt;浏览器解析响应报⽂并使⽤渲染引擎显示到界⾯</p>
<h3 id="⼿机App："><a href="#⼿机App：" class="headerlink" title="⼿机App："></a>⼿机App：</h3><p>⽤户点击或界⾯⾃动触发联⽹需求-&gt; Android代码调⽤拼装HTTP报⽂并发送请求到服务器-&gt;服务器处理请求后发送响应报⽂给⼿机-&gt; Android代码处理响应报⽂并作出相应处理（如储存数据、加⼯数据、显示数据到界⾯）</p>
<h2 id="URL和HTTP报⽂"><a href="#URL和HTTP报⽂" class="headerlink" title="URL和HTTP报⽂"></a>URL和HTTP报⽂</h2><h3 id="URL格式"><a href="#URL格式" class="headerlink" title="URL格式"></a>URL格式</h3><p>三部分：协议类型、服务器地址(和端⼝号)、路径(Path)协议类型://服务器地址[:端⼝号]路径</p>
<p><a href="http://zhangpingV5.top/users?gender=male" target="_blank" rel="noopener">http://zhangpingV5.top/users?gender=male</a></p>
<h2 id="报⽂格式"><a href="#报⽂格式" class="headerlink" title="报⽂格式"></a>报⽂格式</h2><h3 id="请求报⽂"><a href="#请求报⽂" class="headerlink" title="请求报⽂"></a>请求报⽂</h3><h3 id="响应报⽂"><a href="#响应报⽂" class="headerlink" title="响应报⽂"></a>响应报⽂</h3><h2 id="Request-Method请求⽅法"><a href="#Request-Method请求⽅法" class="headerlink" title="Request Method请求⽅法"></a>Request Method请求⽅法</h2><h3 id="GET"><a href="#GET" class="headerlink" title="GET"></a>GET</h3><ul>
<li>⽤于获取资源</li>
<li>对服务器数据不进⾏修改</li>
<li>不发送Body</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET  &#x2F;users&#x2F;1  HTTP&#x2F;1.1</span><br><span class="line">Host: api.github.com</span><br></pre></td></tr></table></figure>

<p>对应Retroﬁt的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">@GET(&quot;&#x2F;users&#x2F;&#123;id&#125;&quot;)</span><br><span class="line">Call&lt;User&gt;getUser(@Path(&quot;id&quot;)Stringid,@Query(&quot;gender&quot;)Stringgender);</span><br></pre></td></tr></table></figure>

<h3 id="POST"><a href="#POST" class="headerlink" title="POST"></a>POST</h3><ul>
<li>⽤于增加或修改资源</li>
<li>发送给服务器的内容写在Body⾥⾯</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST  &#x2F;users  HTTP&#x2F;1.1</span><br><span class="line">Host: api.github.com</span><br><span class="line">Content-Type: application&#x2F;x-www-form-urlencoded</span><br><span class="line">Content-Length: 13</span><br><span class="line"></span><br><span class="line">name&#x3D;rengwuxian&amp;gender&#x3D;male</span><br></pre></td></tr></table></figure>

<p>对应Retroﬁt的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@FormUrlEncoded</span><br><span class="line">@POST(&quot;&#x2F;users&quot;)</span><br><span class="line">Call&lt;User&gt; addUser( @Field(&quot;name&quot;) String name,</span><br><span class="line">@Field(&quot;gender&quot;)Stringgender);</span><br></pre></td></tr></table></figure>


<h3 id="PUT"><a href="#PUT" class="headerlink" title="PUT"></a>PUT</h3><ul>
<li>⽤于修改资源</li>
<li>发送给服务器的内容写在Body⾥⾯</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PUT  &#x2F;users&#x2F;1  HTTP&#x2F;1.1</span><br><span class="line">Host: api.github.com</span><br><span class="line">Content-Type: application&#x2F;x-www-form-urlencoded</span><br><span class="line">Content-Length: 13</span><br><span class="line"></span><br><span class="line">gender&#x3D;female</span><br></pre></td></tr></table></figure>

<p>对应Retroﬁt的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@FormUrlEncoded</span><br><span class="line">@PUT(&quot;&#x2F;users&#x2F;&#123;id&#125;&quot;)</span><br><span class="line">Call &lt;User&gt; updateGender (@Path(&quot;id&quot;)Stringid,</span><br><span class="line">@Field(&quot;gender&quot;)Stringgender);</span><br></pre></td></tr></table></figure>
        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2021/01/08/Http%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5%E3%80%81%E5%8E%9F%E7%90%86/">https://zhangpingv5.gitee.io/2021/01/08/Http%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5%E3%80%81%E5%8E%9F%E7%90%86/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/%E7%AC%94%E8%AE%B0/"># 笔记</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2021/01/11/%E5%8A%A0%E5%AF%86/">加密</a>
            
            
            <a class="next" rel="next" href="/2020/02/17/Flutter--Card%20%E7%BB%84%E4%BB%B6/">Flutter--Card 组件</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
